# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html
import scrapy
from openpyxl import Workbook
from openpyxl.styles import Font
import datetime

class YmxPipeline(object):
    wb = Workbook()
    ws = wb.active
    ws.append(['排名','标题', '链接'])  # 设置表头

    def process_item(self, item, spider):
        #存储数据
        line = [item['sort'], item['name'], item['url']]  # 把数据中每一项整理出来
        self.ws.append(line)                              # 将数据以行的形式添加到xlsx中
        file_name = './goods_'+ str(datetime.datetime.now().strftime('%Y%m%d%H'))
        self.wb.save(file_name + '.xlsx')  # 保存xlsx文件
        return item
